﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace SPMonsterClone
{
    public partial class Form_SPClone : Form
    {
        public Form_SPClone()
        {
            InitializeComponent();
        }

        private void btn_AddCloneJob_Click(object sender, EventArgs e)
        {
            Form_AddCloneJob frm = new Form_AddCloneJob();
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.Owner = this;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fillJobListGrid();
            }
        }

        private void Form_SPClone_Load(object sender, EventArgs e)
        {
            fillJobListGrid();
        }

        private void fillJobListGrid()
        {
            try
            {
                SQLiteDatabase db = new SQLiteDatabase();
                DataTable myTable;
                myTable = db.GetDataTable("SELECT tbCloneJob.* FROM tbCloneJob");

                dataGridView1.DataSource = null;

                dataGridView1.DataSource = myTable;
                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            catch (Exception fail)
            {
                String error = "The following error has occurred:\n\n";
                error += fail.Message.ToString() + "\n\n";
                MessageBox.Show(error);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != dataGridView1.Columns["Manage"].Index) return;

            Form_AddCloneJob frm = new Form_AddCloneJob(dataGridView1.Rows[e.RowIndex].Cells["JobID"].Value.ToString());
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.Owner = this;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fillJobListGrid();
            }

            //MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells["JobName"].Value.ToString());
        }
    }
}
